// 分数运算单元测试
// Unit tests for Fraction operations

// 测试分数的加法
test "分数加法测试" {
    let a = new(1, 2)
    let b = new(1, 3)
    let c = a + b
    assert_eq(c, new(5, 6)) // 1/2 + 1/3 = 5/6

    
}

// 测试分数的减法
test "分数减法测试" {
    let a = new(3, 4)
    let b = new(1, 4)
    let c = a - b
    assert_eq(c, new(1, 2)) // 3/4 - 1/4 = 1/2
}

// 测试分数的乘法
test "分数乘法测试" {
    let a = new(2, 3)
    let b = new(3, 4)
    let c = a * b
    assert_eq(c, new(1, 2)) // 2/3 * 3/4 = 6/12 = 1/2
}

// 测试分数的除法
test "分数除法测试" {
    let a = new(3, 5)
    let b = new(2, 7)
    let c = a / b
    assert_eq(c, new(21, 10)) // 3/5 ÷ 2/7 = 21/10
}

// 测试约分和负数
test "约分和负数测试" {
    let a = new(-2, 4)
    assert_eq(a, new(-1, 2))
    let b = new(6, -8)
    assert_eq(b, new(-3, 4))
    let c = new(8, 4)
    assert_eq(c, new(2, 1))
}

// 测试从字符串解析分数
test "字符串解析测试" {
    // 测试整数
    let a = from_str("42")
    assert_eq(a, new(42, 1))
    
    // 测试分数
    let b = from_str("3/4")
    assert_eq(b, new(3, 4))
    
    // 测试小数
    let c = from_str("0.5")
    assert_eq(c, new(1, 2))
    
    let d = from_str("3.14")
    assert_eq(d, new(157, 50)) // 3.14 = 314/100 = 157/50
    
    // 测试负数
    let e = from_str("-2.5")
    assert_eq(e, new(-5, 2))
}

// 测试从浮点数转换
test "浮点数转换测试" {
    let a = from_float(0.5)
    assert_eq(a, new(1, 2))
    
    let b = from_float(2.0)
    assert_eq(b, new(2, 1))
}

// 测试平方运算
test "平方运算测试" {
    let a = new(3, 4)
    let b = a.square()
    assert_eq(b, new(9, 16)) // (3/4)² = 9/16
    
    let c = new(-2, 3)
    let d = c.square()
    assert_eq(d, new(4, 9)) // (-2/3)² = 4/9
}

// 测试开方运算
test "开方运算测试" {
    let a = new(4, 9)
    let b = a.sqrt()
    // 由于开方可能有精度问题，我们检查结果是否接近期望值
    let expected = new(2, 3) // √(4/9) = 2/3
    let diff = (b.to_double() - expected.to_double()).abs()
    assert_true(diff < 0.001) // 允许小的误差
    
    let c = new(1, 4)
    let d = c.sqrt()
    let expected2 = new(1, 2) // √(1/4) = 1/2
    let diff2 = (d.to_double() - expected2.to_double()).abs()
    assert_true(diff2 < 0.001)
}

// 测试转换为浮点数
test "转换为浮点数测试" {
    let a = new(1, 2)
    assert_eq(a.to_double(), 0.5)
    
    let b = new(3, 4)
    assert_eq(b.to_double(), 0.75)
    
    let c = new(-1, 3)
    let expected = -1.0 / 3.0
    let diff = (c.to_double() - expected).abs()
    assert_true(diff < 0.0001)
}

// 测试倒数运算
test "倒数运算测试" {
    let a = new(3, 4)
    let b = a.reciprocal()
    assert_eq(b, new(4, 3))
    
    let c = new(-2, 5)
    let d = c.reciprocal()
    assert_eq(d, new(5, -2))
    assert_eq(d, new(-5, 2))
}

// 测试绝对值运算
test "绝对值运算测试" {
    let a = new(-3, 4)
    let b = a.abs()
    assert_eq(b, new(3, 4))
    
    let c = new(5, -7)
    let d = c.abs()
    assert_eq(d, new(5, 7))
}

// 测试幂运算
test "幂运算测试" {
    let a = new(2, 3)
    let b = a.pow(2)
    assert_eq(b, new(4, 9))
    
    let c = a.pow(-1)
    assert_eq(c, new(3, 2))
    
    let d = a.pow(0)
    assert_eq(d, new(1, 1))
    
    let e = new(3, 2)
    let f = e.pow(3)
    assert_eq(f, new(27, 8))
}

// 测试带分数转换
test "带分数转换测试" {
    let a = new(7, 3)
    let (integer, fraction) = a.to_mixed_number()
    assert_eq(integer, 2)
    assert_eq(fraction, new(1, 3))
    
    let b = new(5, 2)
    let (integer2, fraction2) = b.to_mixed_number()
    assert_eq(integer2, 2)
    assert_eq(fraction2, new(1, 2))
    
    let c = new(3, 4)
    let (integer3, fraction3) = c.to_mixed_number()
    assert_eq(integer3, 0)
    assert_eq(fraction3, new(3, 4))
}

// 测试百分比转换
test "百分比转换测试" {
    let a = new(1, 2)
    let percent = a.to_percentage()
    assert_eq(percent, "50%")
    
    let b = new(3, 4)
    let percent2 = b.to_percentage()
    assert_eq(percent2, "75%")
}

// 测试分数类型判断
test "分数类型判断测试" {
    let a = new(3, 4)
    assert_true(a.is_proper())
    assert_false(a.is_improper())
    
    let b = new(5, 4)
    assert_false(b.is_proper())
    assert_true(b.is_improper())
    
    let c = new(4, 4)
    assert_false(c.is_proper())
    assert_true(c.is_improper())
}

// 测试比较运算
test "比较运算测试" {
    let a = new(1, 2)
    let b = new(2, 3)
    let c = new(3, 6)
    
    assert_true(a < b)
    assert_true(b > a)
    assert_true(a <= c)
    assert_true(a >= c)
    assert_true(a == c)
    assert_false(a == b)
    
    let d = new(-1, 2)
    let e = new(1, 3)
    assert_true(d < e)
    assert_true(e > d)
}

// 测试符号获取
test "符号获取测试" {
    let a = new(3, 4)
    assert_eq(a.sign(), 1)
    
    let b = new(-2, 5)
    assert_eq(b.sign(), -1)
    
    let c = new(0, 1)
    assert_eq(c.sign(), 0)
}

// 测试相反数
test "相反数测试" {
    let a = new(3, 4)
    let b = a.negate()
    assert_eq(b, new(-3, 4))
    
    let c = new(-2, 5)
    let d = c.negate()
    assert_eq(d, new(2, 5))
}
